/*=======================================================================================
	GoogleTrends_Searches_for_APFD_2004-2017.do

		Plots differences in monthly Google searches for the term 'Permanent Fund'

	Author: Lorenz Kueng, December 2017
=========================================================================================*/

cap n log close Google
log using "$homedir/log-files/GoogleTrends_$date.log", text replace name(Google)


*** Data

use "$homedir/data/stata/Google Trends for APFD_2004-2017.dta", clear

	destring _all, replace
	compress

	generate ym = ym(year,month)
	format   ym %tm
	tsset ym

	xi i.month, noomit
	rename _I* D_*
	
	drop D_month_12 // normalize December to zero


*** Regression

gen ln_permanentfundalaska = log(permanentfundalaska)

reg ln_permanentfundalaska D_month* ym if year!=2008, robust


	*** Plot 
	
	cap drop beta* se m
	gen beta=0
	gen se=0
	gen m=12
	forvalues m=1(1)11{
		cap n replace beta=_b[D_month_`m'] in `m'
		cap n replace se =_se[D_month_`m'] in `m'
		cap n replace m = `m' in `m'
	}
	gen beta_up   = beta + 1.96*se
	gen beta_down = beta - 1.96*se

	cap label drop month
	label define month	1 "jan" ///
						2 "feb" ///
						3 "mar" ///
						4 "apr" ///
						5 "may" ///
						6 "jun" ///
						7 "jul" ///
						8 "aug" ///
						9 "sep" ///
					   10 "oct" ///
					   11 "nov" ///
					   12 "dec" 
	label value m month
	label variable beta "ln(Google search intensity)"
	twoway connected beta       m, lw(medium) lp(solid) msymbol(Oh) mcolor(black)  lc(black) || ///
		   line      beta_up    m, lw(thin)   lp(dash) lc(black)  || ///
		   line      beta_down  m, lw(thin)   lp(dash) lc(black)  || ///	   
		, ///
		  graphregion(color(white)) ///
		  legend(order(1) col(1) ring(0) pos(6) region(lwidth(none))) ///
		  xtitle("") xlabel(1(1)12, valuelabel) ///
		  ylabel(, angle(0)) /// 
		  ytitle("") /// 
		  yline( 0, lc(black) lw(thin)) /// 
		  xline(10, lc(blue) lp(shortdash) lw(medium))
		  
		local file "$homedir/results/figures/GoogleSearch_monthly_LinearTrend_permanentfundalaska"
		graph export "`file'.eps", replace
		cap rm       "`file'.pdf"
		!epstopdf    "`file'.eps"
		graph export "`file'.tif", replace

log close Google
